$(function() {
	// 初始化加载所有年级数据
	readygrade();
	$("#save").click(function(){
		$.ajax({
			url : "/blackhouse/add_GradeServlet",
			type:"post",
			data:{"type":2,"gradeName":$("#addgrade").val()},
			dataType:"json",
			success:function(e){
				if(e==1){
					alert("保存成功");
					readygrade();
					}
				else if(e==0)
					alert("保存失败");
				else if(e==-1)
					alert("服务器故障");
			}
		});
	});
	$("#save-1").click(function(){
		var html = "";
		html += "<tr>";
		html += "<td><input type='checkbox'/></td>";
		html += "<td></td>";
		html += "<td><input id=\"addgrade\" type='text'style='outline: none;border: none;'></td>";
		html += "<td></td>";
		html += "<td></td>";
		html += "<td></td>";
		html += "</tr>";
		$("#tbody").append(html);
	})

});
function readygrade(){
	$.ajax({
		url : "/blackhouse/add_GradeServlet",
		type : "post",
		data : {
			"type" : 1
		},
		dataType : "json",
		success : function(e) {
			var html = "";
			for (var i = 0; i < e.length; i++) {
				html += "<tr name=\""+e[i].id+"\">";
				html += "<td><input type='checkbox'/ ></td>";
				html += "<td>"+e[i].gradeCode+"</td>";
				html += "<td>"+e[i].gradeName+"</td>";
				html += "<td>"+e[i].createMessage+"</td>";
				html += "<td>"+e[i].createTime+"</td>";
				html += "<td><button type=\"button\" class=\"btn btn-success-1\" name=\"delete\">删除</button></td>";
				html += "</tr>";
			}
			$("#tbody").html(html);
			$("#tbody button").click(function (){
				 var id = $(this).parent().parent().attr('name');
			        if ($(this).attr("name") == "delete") {
			            Delete(id);
			        }   
			});
		}
	});
}
//删除年级信息
function Delete(id){
	$.ajax({
		url : "/blackhouse/add_GradeServlet",
		type : "post",
		data : {
			"type" : 3,
			"id":id
		},
		dataType : "json",
		success : function(e) {
			if(e==1){
				alert("删除成功");
				readygrade();}
			else if(e==0)
				alert("删除失败");
			else if(e==-1)
				alert("服务器故障");
		}
	});
}